BEGIN {
  remember=100
  samples=0
}
function addtosamples(s)
{
  for(r=samples-1;r>=0;r--)
  {
    times[r+1]=times[r]
  }
  times[0]=s
  if(samples<remember)
    samples++

  # Sort data
  filename="unsorteddata." PROCINFO["pid"]
  sortcmd = "sort -n - > " filename
  i=0
  for (r=0;r<samples;r++) {
    print times[r] | sortcmd
  }
  close(sortcmd)
  while ((getline newdata < filename) > 0) {
    sorted[i++] = newdata
  }
  close(filename)
  system("rm " filename)
}
function getaverage()
{ 
  # Calculate median
  if(samples % 2 == 0) {
    lower=(samples/2)
    higher=lower+1
    median=(sorted[lower - 1]+sorted[higher - 1])/2
  }
  else{
    medidx=((samples+1)/2)
    median=sorted[medidx - 1]
  }

  return median
}
function printsamples()
{
  printf "Un:"
  for (r=0;r<samples;r++){
    printf " " times[r]
  }
  printf "\n"

  printf "St:"
  for (r=0;r<samples;r++){
    printf " " sorted[r]
  }
  printf "\n"
}
{
  addtosamples($1)
  print $1, getaverage()
}
